package com.example.springboot.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.springboot.common.Result;
import com.example.springboot.entity.SysMenu;
import com.example.springboot.entity.SysRole;
import com.example.springboot.service.SysMenuService;
import com.example.springboot.service.SysRoleService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 菜单表 前端控制器
 * </p>
 *
 * @author Xz
 * @since 2025-03-10
 */
@RestController
@RequestMapping("/menu")
public class SysMenuController {
    @Resource
    private SysMenuService sysMenuService;
    /**
     * 添加
     */
    @PostMapping("/add")
    public Result<?> add(@RequestBody SysMenu sysMenu) {

        boolean i = sysMenuService.save(sysMenu);
        if (i ) {
            return Result.success();
        } else {
            return Result.error("-1", "添加失败");
        }

    }
    /**
     * 删除学生信息
     */

    @DeleteMapping("/delete/{menuid}")
    public Result<?> delete(@PathVariable Long menuid) {
        QueryWrapper<SysMenu> query=new QueryWrapper<>();
        query.lambda().eq(SysMenu::getParentId,menuid);
        List<SysMenu> list=sysMenuService.list(query);
        if (list.size()>0){
            return Result.error("-1","删除失败,该菜单存在下级菜单！");
        }
        boolean i = sysMenuService.removeById(menuid);
        if (i ) {
            return Result.success();
        } else {
            return Result.error("-1", "删除失败");
        }
    }
    //查询列表
    @GetMapping("/list")
    public  Result<?> getList(){
        List<SysMenu> list =sysMenuService.getList();
        return Result.success(list);
    }
    //上级菜单
    @GetMapping("/parent")
    public  Result<?> getParentList(){
        List<SysMenu> list =sysMenuService.getParentList();
        return Result.success(list);
    }

    /**
     * 分页条件查询  根据角色名称查询
     */
    @GetMapping("/find")
    public Result<?> findPage(@RequestParam(defaultValue = "1") Integer pageNum,
                              @RequestParam(defaultValue = "10") Integer pageSize,
                              @RequestParam(defaultValue = "") String search) {
        List<SysMenu> list = sysMenuService.find(pageNum, pageSize, search);
        return Result.success(list);
    }
    /**
     * 更新学生信息
     */
    @PutMapping("/update")
    public Result<?> update(@RequestBody  SysMenu sysMenu) {
        boolean i = sysMenuService.updateById(sysMenu);
        if (i) {
            return Result.success();
        } else {
            return Result.error("-1", "更新失败");
        }
    }

    @GetMapping("/getMenuByUserId")
    public Result<?> update(Long userId) {
        List i = sysMenuService.getMenuByUserId(userId);
        return Result.success(i);
    }

}

